﻿using KPI.Data;
using KPI.Entity;
using System;
using System.Collections.Generic;
using System.Text;
using TYBase;

namespace KPI.Api
{
    public class LocalHttp : TYHttp
    {
        public LocalHttp()
        {
            ContentType = "application/json";

        }
        //public override bool Excute(out string result)
        //{
        //    if (KPIData.user != null && KPIData.user.token.NotNull())
        //    {
        //        Addheader("authorization", "Bearer " + KPIData.user.token);
        //    }
        //    return base.Excute(out result);
        //}

        private string BuildFormData(Dictionary<string, object> formData)
        {
            string boundary = "----FormBoundary";
            StringBuilder bodyBuilder = new StringBuilder();

            foreach (var field in formData)
            {
                bodyBuilder.AppendLine(boundary);
                bodyBuilder.AppendLine($"Content-Disposition: form-data; name=\"{field.Key}\"");
                bodyBuilder.AppendLine();
                bodyBuilder.AppendLine(field.Value.ToString());
            }

            bodyBuilder.AppendLine(boundary + "--");

            return bodyBuilder.ToString();
        }

        public override bool Excute(out string res)
        {
            res = null;
            if (KPIData.user != null && KPIData.user.tokenId.NotNull())
            {
                Addheader("authorization", "Bearer " + KPIData.user.tokenId);

            }
            Addheader("hospitalId", KPIData.hospitalId);
            if (!base.Excute(out var result))
            {
                return false;
            }
            res = result;
            var vo = result.AsModel<VOBase>();
            if (vo != null)
            {
                res = SecurityUtil.decryptSm4Base64(vo.data, KPIData.appSecret);
                return true;
            }
            return false;
        }
        public override bool UploadFile(string filePath, out string result)
        {
            if (KPIData.user != null && KPIData.user.tokenId.NotNull())
            {
                Addheader("authorization", "Bearer " + KPIData.user.tokenId);
            }
            return base.UploadFile(filePath, out result);
        }

        public LocalHttp SetGetData(object dto)
        {
            var dic = dto.AsDict();
            var formData = SecurityUtil.encryptReqData(dic, KPIData.appid, KPIData.appSecret, true);

            //var str = BuildFormData(data);
            //byte[] requestBodyBytes = Encoding.UTF8.GetBytes(str);
            //string json = formData.JsonAsString();
            StringBuilder formDataBuilder = new StringBuilder();
            foreach (var item in formData)
            {
                formDataBuilder.Append($"{item.Key}={Uri.EscapeDataString(item.Value.ToString())}&");
            }
            string formDataString = formDataBuilder.ToString().TrimEnd('&');
            SetGet().SetQueryUrl(formDataString);
            return this;
        }
        public LocalHttp SetData(object requestDto)
        {
            var dic = requestDto.AsDict();
            var formData = SecurityUtil.encryptReqData(dic, KPIData.appid, KPIData.appSecret, true);


            ////var str = BuildFormData(data);
            ////byte[] requestBodyBytes = Encoding.UTF8.GetBytes(str);
            //string json = formData.JsonAsString();
            //StringBuilder formDataBuilder = new StringBuilder();
            //foreach (var item in formData)
            //{
            //    formDataBuilder.Append($"{item.Key}={Uri.EscapeDataString(item.Value.ToString())}&");
            //}
            //string formDataString = formDataBuilder.ToString().TrimEnd('&');
            base.SetData(formData.JsonAsString());
            return this;
        }
    }
}
